home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Special 16 / AMIGAplus Sonderheft 16 (1998)(ICP)(DE)[!].iso / pd / anwendungen / rtgmaster_dev / goodies / rtggadtools / example.c < prev    next >
C/C++ Source or Header  |  1997-01-30  |  14KB  |  486 lines

  1. /*
  2.  
  3.     RTG GadTools Library Demo
  4.     based on
  5.     RTG Library Usage - Skeleton Code
  6.  
  7.     This code © 1997 by Hans-Jörg and Thomas Frieden
  8.     You may use any or all of this code in your own applications.
  9.     Use this code at your own risk. It isn't guaranteed to do
  10.     anything useful. Consider this a random sequence of characters
  11.     that, by chance, can be compiled as a C program.
  12.  
  13.     Requires RTGMaster system to be installed on your system.
  14.  
  15. */
  16.  
  17. /* Generic "Include Everything"-type file */
  18. #include <global.h>
  19. #include "rtggadtools.h"
  20. #include "rtggadtools_protos.h"
  21. #include "rtggadtools_pragmas.h"
  22.  
  23.  
  24. struct RtgScreen *RtgScreen;
  25. struct ScreenReq *sr;
  26. struct RTGMasterBase *RTGMasterBase;
  27. struct Library *UtilityBase, *IntuitionBase;
  28. struct Library *GfxBase, *RTGGadToolsBase;
  29. struct TagItem rtag[] = {
  30.     smr_ChunkySupport,  -1,
  31.     smr_PlanarSupport,  -1,
  32.     //smr_Buffers,        1,
  33.     TAG_DONE,           NULL
  34. };
  35.  
  36. struct TagItem gtag[] = {
  37.     grd_BytesPerRow,    0,
  38.     grd_Width,          0,
  39.     grd_Height,         0,
  40.     grd_Depth,          0,
  41.     grd_PixelLayout,    0,
  42.     grd_ColorSpace,     0,
  43.     grd_PlaneSize,      0,
  44.     grd_BusSystem,      0,
  45.     TAG_DONE,           0
  46. };
  47.  
  48. struct TagItem tacks[] = {
  49.     TAG_DONE,0
  50. };
  51.  
  52. BOOL Planar;
  53. UBYTE *cbuf=NULL;
  54. ULONG width;
  55. UBYTE *sadr;
  56. ULONG cmap[1000];
  57. #define CBUF 76800
  58. BOOL Direct;
  59.  
  60.  
  61. UBYTE Arrow[] = {
  62.     0,0,0,0,0,0,0,0,
  63.     0,0,0,0,255,0,0,0,
  64.     0,0,0,0,255,255,0,0,
  65.     0,255,255,255,255,255,255,0,
  66.     0,255,255,255,255,255,255,0,
  67.     0,0,0,0,255,255,0,0,
  68.     0,0,0,0,255,0,0,0,
  69.     0,0,0,0,0,0,0,0
  70. };
  71.  
  72. UBYTE ArrowA[] = {
  73.     0,0,0,0,0,0,0,0,
  74.     0,0,0,0,128,0,0,0,
  75.     0,0,0,0,128,128,0,0,
  76.     0,128,128,128,128,128,128,0,
  77.     0,128,128,128,128,128,128,0,
  78.     0,0,0,0,128,128,0,0,
  79.     0,0,0,0,128,0,0,0,
  80.     0,0,0,0,0,0,0,0
  81. };
  82.  
  83.  
  84. ULONG Arrow24[] = {
  85.     0x000000,0x000000,0x000000,0x000000,0x000000,0x000000,0x000000,0x000000,
  86.     0x000000,0x000000,0x000000,0x000000,0xffffff,0x000000,0x000000,0x000000,
  87.     0x000000,0x000000,0x000000,0x000000,0xffffff,0xffffff,0x000000,0x000000,
  88.     0x000000,0xffffff,0xffffff,0xffffff,0xffffff,0xffffff,0xffffff,0x000000,
  89.     0x000000,0xffffff,0xffffff,0xffffff,0xffffff,0xffffff,0xffffff,0x000000,
  90.     0x000000,0x000000,0x000000,0x000000,0xffffff,0xffffff,0x000000,0x000000,
  91.     0x000000,0x000000,0x000000,0x000000,0xffffff,0x000000,0x000000,0x000000,
  92.     0x000000,0x000000,0x000000,0x000000,0x000000,0x000000,0x000000,0x000000
  93. };
  94.  
  95. ULONG Arrow24A[] = {
  96.     0x000000,0x000000,0x000000,0x000000,0x000000,0x000000,0x000000,0x000000,
  97.     0x000000,0x000000,0x000000,0x000000,0xffff00,0x000000,0x000000,0x000000,
  98.     0x000000,0x000000,0x000000,0x000000,0xffff00,0xffff00,0x000000,0x000000,
  99.     0x000000,0xffff00,0xffff00,0xffff00,0xffff00,0xffff00,0xffff00,0x000000,
  100.     0x000000,0xffff00,0xffff00,0xffff00,0xffff00,0xffff00,0xffff00,0x000000,
  101.     0x000000,0x000000,0x000000,0x000000,0xffff00,0xffff00,0x000000,0x000000,
  102.     0x000000,0x000000,0x000000,0x000000,0xffff00,0x000000,0x000000,0x000000,
  103.     0x000000,0x000000,0x000000,0x000000,0x000000,0x000000,0x000000,0x000000
  104. };
  105. APTR Anchor;
  106. RGGadget g1,g2,g3,g4;
  107. void *Font;
  108. struct TextAttr ta = {
  109.     "topaz.font",
  110.     8, FS_NORMAL,
  111.     FPF_ROMFONT
  112. };
  113.  
  114. void fail(void) {
  115.     if (RtgScreen) CloseRtgScreen(RtgScreen);
  116.     if (RTGMasterBase) CloseLibrary((struct Library *)RTGMasterBase);
  117.     if (UtilityBase) CloseLibrary(UtilityBase);
  118.     if (RTGGadToolsBase) CloseLibrary(RTGGadToolsBase);
  119.     if (cbuf) FreeVec(cbuf);
  120.     exit(0L);
  121. }
  122. /*
  123.     UpAction Hook
  124.     This code will be called when we get an UpAction from gadget 2. It is
  125.     used to toggle the state of gadget 1 (The toggle button)
  126. */
  127. __asm __saveds void UpAction(register __a0 RGGadget gadget) {
  128.     RGGadgetState(g1, RGS_Toggle);
  129.     RGRefreshGList(Anchor, g1, 1);
  130. }
  131.  
  132.  
  133. /*
  134.     Custom HitTest
  135.     This HitTest routine is used for the little arrow gadget. It actually
  136.     checks to see if the mouse click we received hit one of the non-null
  137.     parts of the image. If it did, it allows the gadget to be hit, other-
  138.     wise rejects the click. This is especially usefull for shaped buttons.
  139.  
  140.  
  141. */
  142. __asm __saveds BOOL HitTest(register __a0 RGGadget gad,
  143.                 register __d0 WORD x,
  144.                 register __d1 WORD y) {
  145.     UBYTE *p;
  146.  
  147.     if (x<gad->LeftEdge || x>=gad->LeftEdge+gad->Width) return FALSE;
  148.     if (y<gad->TopEdge  || y>=gad->TopEdge+gad->Height) return FALSE;
  149.     x-=gad->LeftEdge; y-=gad->TopEdge;
  150.  
  151.     p=(UBYTE *)(gad->GadgetRender)+x+y*gad->Width;
  152.     if (*p) return TRUE;
  153.     else return FALSE;
  154.  
  155. }
  156.  
  157. void main(int argc, char **argv) {
  158.    /*
  159.     * Since this is a demo, I don't check anything at all
  160.     * and simply assume that every open went ok... 8-)
  161.     */
  162.     int i,x;
  163.     struct TagItem *tag;
  164.     UBYTE rr, rg, rb;
  165.     ULONG size;
  166.     ULONG class;
  167.     ULONG hp, lp, back, fore, text;
  168.     struct IntuiMessage *msg;
  169.     char str[512];
  170.     ULONG retval;
  171.     ULONG img, imga;
  172.  
  173.     /*
  174.     For the most part the following code is taken from my code
  175.     example for the rtgmaster library. Look into this archive for
  176.     what the code does.
  177.     */
  178.  
  179.     RTGMasterBase = (struct RTGMasterBase *)OpenLibrary((STRPTR)"rtgmaster.library", 0);
  180.     UtilityBase = OpenLibrary((STRPTR)"utility.library", 37L);
  181.     IntuitionBase = OpenLibrary("intuition.library", 37L);
  182.     GfxBase = OpenLibrary("graphics.library", 37L);
  183.     RTGGadToolsBase = OpenLibrary("rtggadtools.library", 0L);
  184.     if (!RTGGadToolsBase) {
  185.     printf("Error opening RTGGadTools library\n");
  186.     fail();
  187.     }
  188.  
  189.  
  190.     sr = RtgScreenModeReq(rtag);
  191.  
  192.     if (sr==NULL) fail();
  193.  
  194.     RtgScreen = OpenRtgScreen(sr, tacks);
  195.  
  196.     GetRtgScreenData(RtgScreen, gtag);
  197.  
  198.     tag=FindTagItem(grd_BytesPerRow, gtag);
  199.     size = tag->ti_Data;
  200.  
  201.     tag=FindTagItem(grd_Width, gtag);
  202.     width = tag->ti_Data;
  203.  
  204.     tag=FindTagItem(grd_PixelLayout, gtag);
  205.     printf("Screen pixel layout is ");
  206.     switch(tag->ti_Data) {
  207.     case grd_PLANAR:        printf("planar\n"); break;
  208.     case grd_PLANATI:       printf("interleaved planar\n"); break;
  209.     case grd_CHUNKY:        printf("8-Bit Z-Ordered (chunky)\n"); break;
  210.     case grd_HICOL15:       printf("15-Bit Chunky (2 Byte/pixel)\n"); break;
  211.     case grd_HICOL16:       printf("16-Bit Chunky (2 Byte/pixel)\n"); break;
  212.     case grd_TRUECOL24:     printf("24-Bit Chunky (3 Byte/pixel)\n"); break;
  213.     case grd_TRUECOL24P:    printf("24-Bit Chunky (3 Byteplanes/pixel)\n"); break;
  214.     case grd_TRUECOL32:     printf("24-Bit Chunky (4 Bytes/pixel)\n"); break;
  215.     case grd_GRAFFITI:      printf("Graffiti 8 bit\n"); break;
  216.     case grd_TRUECOL32B:    printf("24-Bit Chunky (4 Bytes/pixel)\n"); break;
  217.     default:                printf("unknown (%d)\n", tag->ti_Data); break;
  218.     }
  219.  
  220.     tag=FindTagItem(grd_ColorSpace, gtag);
  221.     if (tag->ti_Data) {
  222.     if (tag->ti_Data == grd_Palette) Direct=FALSE; else Direct=TRUE;
  223.     printf("Color space is ");
  224.     switch(tag->ti_Data) {
  225.         case grd_Palette:   printf("CLUT-Based\n"); break;
  226.         case grd_RGB:       printf("RGB (low-endian RGB)\n"); break;
  227.         case grd_BGR:       printf("BGR (high-endian RGB)\n"); break;
  228.         default:            printf("unknown (%d)\n", tag->ti_Data); break;
  229.     }
  230.     }
  231.  
  232.     tag=FindTagItem(grd_BusSystem, gtag);
  233.     if (tag->ti_Data) {
  234.     printf("Graphics card bus is ");
  235.     switch(tag->ti_Data) {
  236.         case grd_Z3:        printf("Zorro III\n"); break;
  237.         case grd_Z2:        printf("Zorro II\n"); break;
  238.         case grd_Custom:    printf("default custom chips\n"); break;
  239.         case grd_RGBPort:   printf("conneted to the RGB Port\n"); break;
  240.         case grd_GVP:       printf("GVP Special Bus (EGS110)\n"); break;
  241.         case grd_DDirect:   printf("DracCo® Direct\n"); break;
  242.         default:            printf("an unknown bus system\n"); break;
  243.     }
  244.     }
  245.  
  246.     if (tag->ti_Data == grd_PLANAR) Planar = TRUE;
  247.     else Planar = FALSE;
  248.  
  249.     printf("Screen is %ld x %ld x %ld\n", gtag[1].ti_Data, gtag[2].ti_Data, gtag[3].ti_Data);
  250.     printf("It has %ld bytes per row\n", size);
  251.  
  252.  
  253.     if (Planar == TRUE) {
  254.     cbuf = AllocVec(CBUF, MEMF_CLEAR|MEMF_FAST);
  255.     if (cbuf==NULL) {
  256.         cbuf=AllocVec(CBUF, MEMF_CLEAR);
  257.         if (cbuf==NULL) {
  258.         printf("Out of memory *SIGH*\n");
  259.         fail();
  260.         }
  261.     }
  262.     }
  263.  
  264.     if (Direct == FALSE) {
  265.     cmap[0] = 256 * 65536;
  266.     rr = 0;
  267.     rg = 0;
  268.     rb = 0;
  269.     x = 1;
  270.     for (i = 0; i < 256; i++) {
  271.         if (i==17) {
  272.         cmap[x++] = 0xFFFFFFFF;
  273.         cmap[x++] = 0xFFFFFFFF;
  274.         cmap[x++] = 0xFFFFFFFF;
  275.         } else {
  276.         cmap[x++] = (i << 24) | 0xffffff;
  277.         cmap[x++] = (i << 24) | 0xffffff;
  278.         cmap[x++] = (i << 24) | 0xffffff;
  279.         }
  280.     }
  281.     cmap[x++]=0;
  282.     LockRtgScreen(RtgScreen);
  283.     LoadRGBRtg(RtgScreen, (APTR) cmap);
  284.     UnlockRtgScreen(RtgScreen);
  285.     }
  286.  
  287.     if (RtgScreen) {
  288.     /*
  289.         We have to create an anchor for the gadget chain. On Intuition
  290.         we usually have a window or Screen for this. Since there's
  291.         no such thing in RTGMaster, we use a custom structure for this.
  292.  
  293.         DO NOT ALLOCATE THIS YOURSELF. Use the RGNewAnchor function.
  294.  
  295.         The Anchor will be on the RtgScreen, with 1 buffer, and optional
  296.         TrueColor support.
  297.  
  298.     */
  299.     Anchor = RGNewAnchor(RtgScreen, 1, Direct);
  300.     /*
  301.         Since the positioning stuff is only guaranteed to work with
  302.         an 8x8 font, we set Topaz/8 on our screen.
  303.  
  304.     */
  305.     Font = RtgOpenFont(RtgScreen, &ta);
  306.     if (Font) {
  307.         RtgSetFont(RtgScreen, Font);
  308.     }
  309.  
  310.     /*
  311.         Initialize some color values depending on the color model.
  312.         RTGGadTools tries to handle everything, but interpretation
  313.         of the values passed depends on the color model of the
  314.         screen, so you have to take care of that.
  315.     */
  316.     if (Direct == FALSE) {
  317.         text    = 255;
  318.         hp      = 255;
  319.         lp      = 50;
  320.         back    = 40;
  321.         fore    = 80;
  322.     } else {
  323.         text    = 0x00ff7733;
  324.         hp      = 0x00ffffff;
  325.         lp      = 0x00202020;
  326.         back    = 0x000055;
  327.         fore    = 0x00888888;
  328.     }
  329.  
  330.     /*
  331.         Create gadget 1.
  332.         This is a toggle-select button. Since we specify a text for it,
  333.         we can leave width/height calculation to RTGGadTools. Note that
  334.         for each gadget, we give unique numbers for Up/Down-Actions.
  335.         That means that we get a downaction nn for gadget n and an UpAction
  336.         n for gadget n. See the Autodocs for details.
  337.     */
  338.     g1 = RGNewGadget(
  339.         RGG_LeftEdge,           10,
  340.         RGG_TopEdge,            10,
  341.         RGG_RenderText,         "Button 1",
  342.         RGG_Flags,              RGF_ToggleSelect,
  343.         RGG_UpAction,           1,
  344.         RGG_DownAction,         11,
  345.         RGG_TextPen,            text,
  346.         RGG_HiPen,              hp,
  347.         RGG_LoPen,              lp,
  348.         RGG_BackColor,          back,
  349.         RGG_HiliteColor,        fore,
  350.     TAG_DONE);
  351.  
  352.     /*
  353.         Gadget 2 will be a normal pushbutton with a custom UpAction.
  354.         UpActions are executed when the mouse button is *released*
  355.         on the gadget, while a DownAction will be called when the
  356.         button is pressed.
  357.     */
  358.     g2 = RGNewGadget(
  359.         RGG_LeftEdge,           40,
  360.         RGG_TopEdge,            30,
  361.         RGG_RenderText,         "Disable/Enable 1",
  362.         RGG_UpAction,           (ULONG)UpAction,
  363.         RGG_Flags,              RGF_UpAction,
  364.         RGG_DownAction,         22,
  365.         RGG_TextPen,            text,
  366.         RGG_HiPen,              hp,
  367.         RGG_LoPen,              lp,
  368.         RGG_BackColor,          back,
  369.         RGG_HiliteColor,        fore,
  370.     TAG_DONE);
  371.  
  372.     /*
  373.         Again, a pushbutton which quits the program.
  374.     */
  375.     g3 = RGNewGadget(
  376.         RGG_LeftEdge,           10,
  377.         RGG_TopEdge,            50,
  378.         RGG_RenderText,         "Quit",
  379.         RGG_UpAction,           3,
  380.         RGG_DownAction,         33,
  381.         RGG_TextPen,            text,
  382.         RGG_HiPen,              hp,
  383.         RGG_LoPen,              lp,
  384.         RGG_BackColor,          back,
  385.         RGG_HiliteColor,        fore,
  386.     TAG_DONE);
  387.  
  388.     if (Direct == TRUE) {
  389.         img     = (ULONG) Arrow24;
  390.         imga    = (ULONG) Arrow24A;
  391.     } else {
  392.         img     = (ULONG) Arrow;
  393.         imga    = (ULONG) ArrowA;
  394.     }
  395.  
  396.     /*
  397.         The Arrow-Shaped button. Since this is an image button, we
  398.         *MUST* specify width and height. See the Autodocs and other
  399.         documentation for the format of the image data.
  400.     */
  401.     g4 = RGNewGadget(
  402.         RGG_LeftEdge,           10,
  403.         RGG_TopEdge,            70,
  404.         RGG_Width,              8,
  405.         RGG_Height,             8,
  406.         RGG_HitTest,            HitTest,
  407.         RGG_RenderImage,        img,
  408.         RGG_SelectRender,       imga,
  409.         RGG_UpAction,           4,
  410.         RGG_DownAction,         44,
  411.         RGG_BackColor,          back,
  412.         RGG_HiliteColor,        fore,
  413.     TAG_DONE);
  414.  
  415.     printf("Created four gadgets at 0x%lx, 0x%lx, 0x%lx, 0x%lx\n",
  416.         g1,g2,g3,g4);
  417.  
  418.     /*
  419.         Now add the gadgets to the Anchor. Normally, you would
  420.         check if the creation was successfull.
  421.     */
  422.     RGAddGadget(Anchor, g1, 0);
  423.     RGAddGadget(Anchor, g2, 0);
  424.     RGAddGadget(Anchor, g3, 0);
  425.     RGAddGadget(Anchor, g4, 0);
  426.  
  427.     LockRtgScreen(RtgScreen);
  428.     sadr = (UBYTE *)GetBufAdr(RtgScreen,0);
  429.     if (Direct) { /* Delete the screen in TrueColor Mode. */
  430.         LONG x;   /* Could have used BlitClear, too */
  431.         for (x=0; x<gtag[2].ti_Data; x++) {
  432.         DrawRtgLineRGB(RtgScreen,sadr,0, 0,x,gtag[1].ti_Data-1,x);
  433.         }
  434.     }
  435.     RtgInitRDCMP(RtgScreen);    /* Start the RDCMP */
  436.     /*
  437.         The following call draws the gadgets for the first time.
  438.         You must do this once. Normally you would open the window your
  439.         gadgets are in (with Intuition), but alas, we don't have one
  440.         here...
  441.     */
  442.     RGRefreshGList(Anchor, NULL, -1);
  443.  
  444.     do {
  445.         msg=RtgGetMsg(RtgScreen); /* Get the RDCMP message */
  446.         /*
  447.         The RGDoGadgets call handles all the stuff. You must call
  448.         this on a regular basis (with every message you get from
  449.         the RDCMP). NULL messages will be ignored, you could also
  450.         have moved the "retval = ..." statement into the if statement
  451.         below.
  452.         */
  453.         retval = RGDoGadgets(Anchor, msg);
  454.         if (msg) {
  455.         class = msg->Class;
  456.         RtgReplyMsg(RtgScreen, msg);
  457.         if (class == IDCMP_RAWKEY) break;
  458.         }
  459.         if (retval == 3) break; /* Quit button was released */
  460.         if (retval != 0) { /* Print what happened */
  461.         sprintf(str, "RetVal: %d     ", retval);
  462.         if (Direct) RtgSetTextModeRGB(RtgScreen, 0xffffff, 0, JAM2);
  463.         else        RtgSetTextMode(RtgScreen, 255, 0, JAM2);
  464.         RtgText(RtgScreen, sadr, str, strlen(str), 5,100);
  465.         }
  466.     } while( 1);
  467.  
  468.     if (Font) RtgCloseFont(RtgScreen, Font);
  469.     /*
  470.         When you're done, dispose the anchor. This automatically
  471.         disposes the gadgets, too, if they are still attached.
  472.     */
  473.     RGDisposeAnchor(Anchor);
  474.  
  475.  
  476.     UnlockRtgScreen(RtgScreen);
  477.     CloseRtgScreen(RtgScreen);
  478.     }
  479.  
  480.     CloseLibrary(GfxBase);
  481.     CloseLibrary((struct Library *)RTGMasterBase);
  482.     CloseLibrary(UtilityBase);
  483.     CloseLibrary(IntuitionBase);
  484. }
  485.  
  486.